package com.getpebble.android.framework.install;

import android.content.Context;
import android.net.Uri;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.install.PebbleBundle;
import com.getpebble.android.framework.util.SideloadingUtils;
import com.getpebble.android.framework.util.SideloadingUtils.BundleType;
import com.google.gson.JsonSyntaxException;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public abstract class PebbleBundleManager<B extends PebbleBundle> {
    private static final String TAG = PebbleBundleManager.class.getSimpleName();
    private Context mContext;
    private final FileDownloadManager mFileDownloadManager;

    public abstract B getBundleFromFile(File file) throws IOException, JsonSyntaxException;

    public abstract String getDirectory();

    public PebbleBundleManager(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        this.mContext = context;
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        this.mFileDownloadManager = new FileDownloadManager(this.mContext, getDirectory());
    }

    public B fetchBundleFromUri(Uri bundleUri) {
        B bundle = null;
        if (bundleUri == null || bundleUri.getScheme() == null) {
            return null;
        }
        Trace.debug(TAG, "fetchBundleFromUri bundleUri = " + bundleUri);
        File file = null;
        boolean fileDownloaded = false;
        if (bundleUri.getScheme().equals("content")) {
            file = SideloadingUtils.downloadContentUriToFile(this.mContext, bundleUri, BundleType.FIRMWARE);
            fileDownloaded = true;
        } else if (bundleUri.getScheme().equals("file")) {
            file = new File(bundleUri.getPath());
        } else if (bundleUri.getScheme().equals("http") || bundleUri.getScheme().equals("https")) {
            file = this.mFileDownloadManager.getFileFromUri(bundleUri.getPath());
        }
        if (file == null) {
            Trace.error(TAG, "File was null, no content scheme matched");
            return null;
        }
        Trace.debug(TAG, "fetchBundleFromUri file = " + file);
        if (!file.exists()) {
            Trace.debug(TAG, "fetchBundleFromUri: File does not exist, downloading...");
            this.mFileDownloadManager.downloadFile(bundleUri.toString());
            fileDownloaded = true;
        }
        try {
            bundle = getBundleFromFile(file);
            if (fileDownloaded) {
                file.delete();
            }
            return bundle;
        } catch (IOException e) {
            Trace.error(TAG, "Failed to get local file for " + file, e);
            if (bundle == null) {
                return null;
            }
            bundle.closeFile();
            return null;
        } catch (JsonSyntaxException jse) {
            if (bundle != null) {
                bundle.closeFile();
            }
            Trace.error(TAG, "Failed to parse bundle.", jse);
            return null;
        }
    }

    protected Map<String, ZipEntry> getComponents(ZipFile zipFile) {
        HashMap<String, ZipEntry> components = new HashMap();
        Enumeration<? extends ZipEntry> zipEntryEnumeration = zipFile.entries();
        while (zipEntryEnumeration.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) zipEntryEnumeration.nextElement();
            components.put(zipEntry.getName(), zipEntry);
        }
        return components;
    }

    public String downloadFile(String uri) {
        return this.mFileDownloadManager.downloadFile(uri);
    }

    public boolean deleteFile(String uri) {
        return this.mFileDownloadManager.deleteFile(uri);
    }
}
